package org.example.NodeList;

import org.example.Tool.Tool;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

public class ExampleNode extends Tool {
    static  ListNode  temp =new ListNode(1);
    public ExampleNode() {
        temp= new ListNode(2,temp);
        temp= new ListNode(3,temp);
        temp= new ListNode(4,temp);
        temp= new ListNode(5,temp);
    }

    /**
     * @apiNote   链表倒序
     * */
    public ListNode reverseNode(ListNode node){
       int[] s = new int[1];
       int i=0;
       while (node!=null){
           s[i++] = node.val;
           node = node.next;
           if(node!=null)
           s = copyarray(s,s.length+1);
       }
        for (int length:s) {
            node = new ListNode(length,node);
        }
        return node;
    }
    /**
     * @apiNote 打印链表
     * */
    public void printNode(ListNode head){
        while (head!=null){
            System.out.println(head.val);
            head = head.next;
        }
    }
    /**
     * @apiNote Flag of Henan
     * @implNote 链表河南国旗实现
     */
    public ListNode Flag(ListNode node,int target){
        ListNode small=null,middle=null,big=null;
        while (node!=null){
            if(node.val>target){
                big = new ListNode(node.val,big);
            }
            else if(node.val<target){
                small = new ListNode(node.val,small);
            }
            else{
                middle = new ListNode(node.val,middle);
            }
            node = node.next;
        }
        ListNode temp=null;
        while(big!=null){
            temp = new ListNode(big.val,temp);
            big = big.next;
        }
        while(middle!=null){
            temp = new ListNode(middle.val,temp);
            middle = middle.next;
        }
        while(small!=null){
            temp = new ListNode(small.val,temp);
            small = small.next;
        }
        return  temp;
    }
    public ListNode reverseBetween(ListNode head, int left, int right) {
        int index = 0;
        ListNode small=null,big=null,temp=null;
        List<Integer> middle = new ArrayList<>();
        while(head!=null){

            if(index<left){
                small = new ListNode(head.val,small);
            }
            else if(index>right){
                big = new ListNode(head.val,big);
            }
            else{
                middle.add(head.val);
            }
            index++;
            head = head.next;

        }
        while(big!=null){
            temp = new ListNode(big.val,temp);
            big = big.next;
        }
        for (Integer integer : middle) {
            temp = new ListNode(integer, temp);
        }
        while(small!=null){
            temp = new ListNode(small.val,temp);
            small = small.next;
        }
        return temp;
    }
    public static void main(String[] args) {
        temp = new ExampleNode().reverseBetween(temp,2,4);
        new ExampleNode().printNode(temp);
    }
}
